home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / lib / getfqdn.c < prev    next >
C/C++ Source or Header  |  1993-03-18  |  1KB  |  61 lines

  1. /*  $Revision: 1.8 $
  2. **
  3. */
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <netdb.h>
  7. #include "configdata.h"
  8. #include "paths.h"
  9. #include "clibrary.h"
  10. #include "libinn.h"
  11.  
  12.  
  13. /*
  14. **  Get the fully-qualified domain name for this host.
  15. */
  16. char *
  17. GetFQDN()
  18. {
  19.     static char        buff[SMBUF];
  20.     struct hostent    *hp;
  21.     char        *p;
  22.     char        temp[SMBUF + 2];
  23.  
  24.     /* Return any old results. */
  25.     if (buff[0])
  26.     return buff;
  27.  
  28.     /* Try gethostname. */
  29.     if (gethostname(buff, (int)sizeof buff) < 0)
  30.     return NULL;
  31.     if (strchr(buff, '.') != NULL)
  32.     return buff;
  33.  
  34.     /* See if DNS (or /etc/hosts) gives us a full domain name. */
  35.     if ((hp = gethostbyname(buff)) == NULL)
  36.     return NULL;
  37.     if (strchr(hp->h_name, '.') == NULL) {
  38.     /* Try to force DNS lookup if NIS/whatever gets in the way. */
  39.     (void)strncpy(temp, buff, sizeof buff);
  40.     (void)strcat(temp, ".");
  41.     hp = gethostbyname(temp);
  42.     }
  43.     if (hp != NULL && strchr(hp->h_name, '.') != NULL) {
  44.     if (strlen(hp->h_name) < sizeof buff - 1)
  45.         return strcpy(buff, hp->h_name);
  46.     /* Doesn't fit; make sure we don't return bad data next time. */
  47.     buff[0] = '\0';
  48.     return hp->h_name;
  49.     }
  50.  
  51.     /* Get the domain name config parameter and append it. */
  52.     if ((p = GetFileConfigValue(_CONF_DOMAIN)) == NULL || *p == '\0')
  53.     return NULL;
  54.     if (strlen(buff) + 1 + strlen(p) > sizeof buff - 1)
  55.     /* Doesn't fit. */
  56.     return NULL;
  57.     (void)strcat(buff, ".");
  58.     (void)strcat(buff, p);
  59.     return buff;
  60. }
  61.